#include "Coor.h"

#include <android/log.h>
#define  TAG    "native-dev"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__)
//
// Created by xxkj on 2019-11-5.
//MTCNN::MTCNN(){}
Coor::Coor(const string &model_path) {

    std::vector<std::string> param_files = {
            model_path+"yolov3.param"
    };

    std::vector<std::string> bin_files = {
            model_path+"yolov3.bin"
    };
    LOGD("init, tModelDir=%s", param_files[0].data());
    LOGD("init, tModelDir=%s", bin_files[0].data());
    //register_darknet_layer(net);
    int res = net.load_param(param_files[0].data());
    LOGD("model param=%d",res);
    res = net.load_model(bin_files[0].data());
    LOGD("model bin=%d",res);
}

Coor::Coor(const std::vector<std::string> param_files, const std::vector<std::string> bin_files){
    net.load_param(param_files[0].data());
    net.load_model(bin_files[0].data());
}


Coor::~Coor(){
    net.clear();
}
int Coor::detect(ncnn::Mat& img_, std::vector<Object>& objects,int oriw, int orih) {
    const float norm_vals[3] = { 1.0/256, 1.0/256, 1.0/256 };
    img_.substract_mean_normalize(0, norm_vals);
    LOGD("chushihua");
    ncnn::Extractor ex = net.create_extractor();

    ex.set_num_threads(this->num_threads);

    ex.input("data", img_);
    LOGD("img_.w=%d,img_.h=%d",img_.w,img_.h);

    //ncnn::Mat out;

    //ex.extract("detection_out", out);

    ncnn::Mat out;

//    ncnn::Blob *out_blob = net.get_last_layer_output_blob();
//    LOGD("%s",out_blob->name.c_str() );

    int result = ex.extract(329, out);
    LOGD("extract");
    if (result != 0)
    {
        printf("ncnn error: %d\n", result);
        return result;
    }
    //     printf("%d %d %d\n", out.w, out.h, out.c);
    objects.clear();
    for (int i = 0; i < out.h; i++)
    {
        const float* values = out.row(i);

        Object object;
        object.label = values[0];
        object.prob = values[1];
        object.rect.x = values[2] * oriw;
        object.rect.y = values[3] * orih;
        object.rect.width = values[4] * oriw - object.rect.x;
        object.rect.height = values[5] * orih - object.rect.y;

        objects.push_back(object);
    }
    return 0;
}
void Coor::SetInputSize(int resSize)
{
    resize_size = resSize;
    return;
}
int Coor::GetInputImgSize()
{
    int size = resize_size;
    return size;
}
void Coor::SetNumThreads(int numThreads)
{
    num_threads = numThreads;
}
void Coor::SetTimeCount(int timeCount)
{
    count = timeCount;
}
//

